<template>
  <!-- 容器-->
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="黑马" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <!-- 主题区 -->
    <el-container>
      <!-- 侧边  当折叠的时候宽度为64，不折叠的时候宽度为200-->
      <el-aside :width="isCollapse ? '64px':'200px'">
        <!-- 绑定事件 -->
        <div class="aside-leter" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409fee"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
          unique-opened
        >
          <el-submenu :index="item.id+''" v-for="item in MenuList" :key="item.id">
            <template slot="title">
              <i :class="MenuIcon[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+item.path"
              v-for="item in item.children"
              :key="item.id"
              @click="getPath('/'+item.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 侧边主题区 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      isCollapse: false,
      activePath: '',
      MenuList: [],
      MenuIcon: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  created() {
    this.getMenuList()
    // 当页面加载的时候跳转的index值，并赋值给activePath，在页面中的动态绑定的:default-active="activePath"可以获取动态值显示不同的样式
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    loginOut() {
      //   点击退出，清除token,并跳转到登录页面
      // window.sessionStorage.clear()
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenuList() {
      // 发送请求，获取左侧菜单的数据
      const res = await this.$http.get('menus', result => {
        return result
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.MenuList = res.data
      // console.log(res)
      // console.log(this.MenuList)
    },
    toggleCollapse() {
      // alert(11)
      this.isCollapse = !this.isCollapse
    },
    getPath(val) {
      // 将跳转的index值保存到sessionStorage中
      window.sessionStorage.setItem('activePath', val)
      this.activePath = val
      console.log(val)
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  padding-left: 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  div {
    display: flex;
    align-items: center;
    font-size: 20px;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    width: 100%;
  }
  .iconfont {
    margin-right: 10px;
  }
  .aside-leter {
    background-color: #ccc;
    height: 35px;
    line-height: 35px;
    font-size: 16px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
